﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_ANTProductManage_TProductType_PIMInfo_Manage : System.Web.UI.Page
{
    public TProductType_PIMInfo tpp;

    public ProductManage_ANTProductManage_TProductType_PIMInfo_Manage()
    {
        tpp = new TProductType_PIMInfo();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTProductType_PIM();
        }
    }

    public void BindTProductType_PIM()
    {
        this.GridView1.DataSource = tpp.LoadEntities();
        this.GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindTProductType_PIM();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindTProductType_PIM();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }    
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string productTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string testItemName = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        TPPIMItem item = new TPPIMItem { ProductTypeID = productTypeID, TestItemName = testItemName };
        tpp.DeleteItem(item);
        BindTProductType_PIM();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindTProductType_PIM();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TProductType_PIMInfoInfo tppInfo = new TProductType_PIMInfoInfo();
        tppInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        tppInfo.TestItemName = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        tppInfo.TestFre = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        tppInfo.TestPort = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString();
        tppInfo.TestAngle = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();

        tpp.Update(tppInfo);
        GridView1.EditIndex = -1;
        BindTProductType_PIM();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        TProductType_PIMInfoInfo tppInfo = new TProductType_PIMInfoInfo();
        tppInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        tppInfo.TestItemName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        tppInfo.TestFre = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
        tppInfo.TestPort = ((TextBox)(d.Rows[3].Cells[1].Controls[0])).Text.Trim();
        tppInfo.TestAngle = ((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim();

        TPPIMItem tppi = new TPPIMItem { ProductTypeID = tppInfo.ProductTypeID, TestItemName = tppInfo.TestItemName };

        TProductType_PIMInfoInfo pro = tpp.LoadEntity(tppi);
        if (pro.ProductTypeID == tppInfo.ProductTypeID)
        {
            string str1 = "此产品类型与测试项目已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            tpp.Insert(tppInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindTProductType_PIM();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindTProductType_PIM();
    }
}
